package cn.iocoder.yudao.module.agri.controller.admin.farmplan;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.agri.controller.admin.farmplan.vo.*;
import cn.iocoder.yudao.module.agri.dal.dataobject.farmplan.FarmPlanDO;
import cn.iocoder.yudao.module.agri.convert.farmplan.FarmPlanConvert;
import cn.iocoder.yudao.module.agri.service.farmplan.FarmPlanService;

@Tag(name = "管理后台 - 种养批次")
@RestController
@RequestMapping("/agri/farm-plan")
@Validated
public class FarmPlanController {

    @Resource
    private FarmPlanService farmPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建种养批次")
    @PreAuthorize("@ss.hasPermission('agri:farm-plan:create')")
    public CommonResult<Long> createFarmPlan(@Valid @RequestBody FarmPlanCreateReqVO createReqVO) {
        return success(farmPlanService.createFarmPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新种养批次")
    @PreAuthorize("@ss.hasPermission('agri:farm-plan:update')")
    public CommonResult<Boolean> updateFarmPlan(@Valid @RequestBody FarmPlanUpdateReqVO updateReqVO) {
        farmPlanService.updateFarmPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除种养批次")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('agri:farm-plan:delete')")
    public CommonResult<Boolean> deleteFarmPlan(@RequestParam("id") Long id) {
        farmPlanService.deleteFarmPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得种养批次")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('agri:farm-plan:query')")
    public CommonResult<FarmPlanRespVO> getFarmPlan(@RequestParam("id") Long id) {
        FarmPlanDO farmPlan = farmPlanService.getFarmPlan(id);
        return success(FarmPlanConvert.INSTANCE.convert(farmPlan));
    }

    @GetMapping("/list")
    @Operation(summary = "获得种养批次列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('agri:farm-plan:query')")
    public CommonResult<List<FarmPlanRespVO>> getFarmPlanList(@RequestParam("ids") Collection<Long> ids) {
        List<FarmPlanDO> list = farmPlanService.getFarmPlanList(ids);
        return success(FarmPlanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得种养批次分页")
    @PreAuthorize("@ss.hasPermission('agri:farm-plan:query')")
    public CommonResult<PageResult<FarmPlanRespVO>> getFarmPlanPage(@Valid FarmPlanPageReqVO pageVO) {
        PageResult<FarmPlanDO> pageResult = farmPlanService.getFarmPlanPage(pageVO);
        return success(FarmPlanConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出种养批次 Excel")
    @PreAuthorize("@ss.hasPermission('agri:farm-plan:export')")
    @OperateLog(type = EXPORT)
    public void exportFarmPlanExcel(@Valid FarmPlanExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FarmPlanDO> list = farmPlanService.getFarmPlanList(exportReqVO);
        // 导出 Excel
        List<FarmPlanExcelVO> datas = FarmPlanConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "种养批次.xls", "数据", FarmPlanExcelVO.class, datas);
    }

}
